package com.littlepay.kr4.paulin.dfeba77;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hanks.passcodeview.PasscodeView;
import com.littlepay.kr4.paulin.dfeba77.config.PasscodeConfig;
import com.littlepay.kr4.paulin.dfeba77.utils.PrefUtils;


public class LockActivity extends AppCompatActivity {
    //Toolbar toolbar;
    //判断是否是应用启动的进入事件
    public static boolean isLogin = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);
        // 尝试获取islogin，但是除了是设置跳转的验证部分，
        // 还有应用本身点击进入，这时候就不需要获取islogin了（此时也正好是空对象引用）
//        try {
//            Intent intent0 = this.getIntent();
//            isLogin = (boolean) intent0.getSerializableExtra("isLogin");
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        //用于isLogin为true，正常进入时跳转
        if (!PrefUtils.getwelcome() && !PrefUtils.getLocked()) {
            //没有上锁而且是第一次以后进入
            Intent intent1 = new Intent(this, MainActivity.class);
            startActivity(intent1);
            finish();
        } else if (PrefUtils.getwelcome()) {
            //是第一次进入的欢迎模式
            Intent intent1 = new Intent(this, GuideActivity.class);
            startActivity(intent1);
            finish();
        }
        //忘记密码的处理
        TextView forgetText = (TextView) findViewById(R.id.forgot);
        forgetText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PrefUtils.setLocked(false);
                PrefUtils.setPassword("0");
                Toast.makeText(getApplication(), "重置密码成功，请退出应用重新进入", Toast.LENGTH_SHORT).show();
            }
        });
//        toolbar = (Toolbar) findViewById(R.id.toolbar_lock_layout);
//        setSupportActionBar(toolbar);
//        if(getSupportActionBar() != null){
//            //getSupportActionBar().setHomeButtonEnabled(true);
//            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//            getSupportActionBar().setDisplayShowTitleEnabled(true);
//            getSupportActionBar().setTitle("输入您的pin码");
//        }
        //用于登录
        final PasscodeView passcodeView_login = (PasscodeView) findViewById(R.id.passcodeView_login);
        passcodeView_login
                .setPasscodeLength(PasscodeConfig.passcodeLength)
                .setLocalPasscode(PrefUtils.getPassword())
                .setListener(new PasscodeView.PasscodeViewListener() {
                    @Override
                    public void onFail() {
                        //Toast.makeText(getApplication(),"密码不正确",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(String number) {
                        //Toast.makeText(getApplication(),"验证通过",Toast.LENGTH_SHORT).show();
                        //如果是从外部进入的，就要进入MainActivity，否则是setting的跳转修改密码，需要先验证已设置的密码，得跳到setting的设置密码里面
                        if (isLogin) {
                            Intent intent = new Intent(LockActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            PrefUtils.setLocked(false);
                            LockSettingActivity.isVerify = true;
                            Intent intent = new Intent(LockActivity.this, LockSettingActivity.class);
                            //LockSettingActivity.isVerify = true;
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("isVerify", true);
                            intent.putExtras(bundle);
                            startActivity(intent);
                            Toast.makeText(getApplication(), "不设置新密码返回即可", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                });
        //用于设置

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
/*        if(keyCode == KeyEvent.KEYCODE_BACK ){

            return true;
        }*/
        return super.onKeyDown(keyCode, event);
    }


}
